package example;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpClient
{
    public static void main(String s[]) throws Exception
    {
        String string = "hello";
        byte bytes[] = string.getBytes();

        DatagramSocket socket = new DatagramSocket();
        InetAddress address = InetAddress.getByName("127.0.0.1");
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 1818);
        socket.send(packet);


        byte bytes2[] = new byte[2000];
        DatagramPacket resultPacket = new DatagramPacket(bytes2, bytes2.length);
        socket.receive(resultPacket);
        String string2 = new String(bytes2, 0, resultPacket.getLength());
        System.out.println("服务端返回："+ string2);

        socket.close();
    }
}





